package com.sinothk.gpserver.modules.biz.controller;

import com.sinothk.gpserver.modules.comm.base.controller.BaseController;
import com.sinothk.gpserver.modules.biz.entity.InviteEntity;
import com.sinothk.gpserver.modules.biz.service.IInviteService;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 1. 好友邀请功能
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-09-11
 */
@Api(tags = "好友邀请功能")
@RequestMapping("/invite")
@ResponseResult()
@RestController
public class InviteController extends BaseController {

    @Resource
    private IInviteService inviteService;

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public PageData<InviteEntity> page(@RequestBody PageVo<InviteEntity> pageVo) {
        return inviteService.page(pageVo);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/get/{id}")
    public InviteEntity get(@ApiParam("id") @PathVariable String id) {
        return inviteService.getById(id);
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping(value = "/add")
    public String add(@RequestParam("groupId") String groupId, @RequestParam("toAccount") String toAccount) {
        InviteEntity inviteEntity = new InviteEntity();
        inviteEntity.setFromBizId(groupId);
        inviteEntity.setToAccount(toAccount);
        inviteService.add(inviteEntity);
        return "新增成功";
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping("/update")
    public InviteEntity update(@RequestBody InviteEntity inviteEntity) {
        if (StringUtil.isEmpty(inviteEntity.getId())) {
            throw new BizException("id不能为空");
        }
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        inviteEntity.setUpdateBy(login.getUserName());
        inviteService.updateById(inviteEntity);
        return inviteService.getById(inviteEntity.getId());
    }

    @ApiOperation(value = "新增/更新", notes = "新增/更新")
    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody InviteEntity inviteEntity) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        if (StringUtil.isEmpty(inviteEntity.getId())) {
            inviteEntity.setCreateBy(login.getAccount());
            inviteService.save(inviteEntity);
            return "新增成功";
        } else {
            inviteEntity.setUpdateBy(login.getUserName());
            inviteService.updateById(inviteEntity);
            return "更新成功";
        }
    }

    @ApiOperation(value = "删除（逻辑删除）", notes = "删除（逻辑删除）")
    @DeleteMapping("/del/{id}")
    public String del(@ApiParam("id") @PathVariable String id) {
        inviteService.removeById(id);
        return "已逻辑删除";
    }

    @ApiOperation(value = "邀请添加或拒绝", notes = "邀请添加或拒绝")
    @PutMapping("/join")
    public String join(@RequestParam("inviteId") String inviteId, @RequestParam("state") int state) {
        if (StringUtil.isEmpty(inviteId)) throw new BizException("inviteId不能为空");
        inviteService.join(inviteId, state);
        return "";
    }

    @ApiOperation(value = "退出俱乐部或删除好友", notes = "退出")
    @GetMapping("/exit")
    public String exit(@RequestParam("bizId") String bizId, @ApiParam("俱乐部/个人") @RequestParam("bizType") String bizType) {

        if (StringUtil.isEmpty(bizId)) throw new BizException("bizId不能为空");
        if (StringUtil.isEmpty(bizType)) throw new BizException("bizType不能为空");

        return inviteService.exit(bizId, bizType);
    }
}
